package com.ruoyi.rms.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.rms.domain.CategoryMessage;
import com.ruoyi.rms.service.ICategoryMessageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 菜品类别管理Controller
 * 
 * @author wyl
 * @date 2024-03-07
 */
@RestController
@RequestMapping("/rms/category")
public class CategoryMessageController extends BaseController
{
    @Autowired
    private ICategoryMessageService categoryMessageService;

    /**
     * 查询菜品类别管理列表
     */
//    @PreAuthorize("@ss.hasPermi('rms:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(CategoryMessage categoryMessage)
    {
        startPage();
        List<CategoryMessage> list = categoryMessageService.selectCategoryMessageList(categoryMessage);
        return getDataTable(list);
    }

    /**
     * 查询菜品类别管理列表 不分页
     */
//    @PreAuthorize("@ss.hasPermi('rms:category:list')")
    @GetMapping("/allData")
    public AjaxResult allData(CategoryMessage categoryMessage)
    {
        return AjaxResult.success(categoryMessageService.selectCategoryMessageList(categoryMessage));
    }

    /**
     * 导出菜品类别管理列表
     */
    @PreAuthorize("@ss.hasPermi('rms:category:export')")
    @Log(title = "菜品类别管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CategoryMessage categoryMessage)
    {
        List<CategoryMessage> list = categoryMessageService.selectCategoryMessageList(categoryMessage);
        ExcelUtil<CategoryMessage> util = new ExcelUtil<CategoryMessage>(CategoryMessage.class);
        util.exportExcel(response, list, "菜品类别管理数据");
    }

    /**
     * 获取菜品类别管理详细信息
     */
//    @PreAuthorize("@ss.hasPermi('rms:category:query')")
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable("categoryId") Long categoryId)
    {
        return success(categoryMessageService.selectCategoryMessageByCategoryId(categoryId));
    }

    /**
     * 新增菜品类别管理
     */
    @PreAuthorize("@ss.hasPermi('rms:category:add')")
    @Log(title = "菜品类别管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CategoryMessage categoryMessage)
    {
        return categoryMessageService.insertCategoryMessage(categoryMessage) > 0 ? AjaxResult.success("添加成功") : AjaxResult.error("类别名称与已有类别名称重复，添加失败");
    }

    /**
     * 修改菜品类别管理
     */
    @PreAuthorize("@ss.hasPermi('rms:category:edit')")
    @Log(title = "菜品类别管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CategoryMessage categoryMessage)
    {
        return categoryMessageService.updateCategoryMessage(categoryMessage) > 0 ? AjaxResult.success("修改成功") : AjaxResult.error("修改后的名称与其他类别名称重复，修改失败");
    }

    /**
     * 删除菜品类别管理
     */
    @PreAuthorize("@ss.hasPermi('rms:category:remove')")
    @Log(title = "菜品类别管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{categoryIds}")
    public AjaxResult remove(@PathVariable Long[] categoryIds)
    {
        return toAjax(categoryMessageService.deleteCategoryMessageByCategoryIds(categoryIds));
    }
}
